package model

import (
	"fmt"

	"github.com/jinzhu/gorm"
)

type TargetInfo struct {
	Code string
	TP   int
	ACC  string
}
type TargetInfoExt struct {
	*TargetInfo
	CheckedCount int `json:"checkedCount"`
}

type TargetList struct {
	List []*TargetInfo
}

type TargetInfoExtListSwagger struct {
	*SwaggerSuccess
	Data []*TargetInfoExt `json:"data"`
}

type TargetListSwagger struct {
	*SwaggerSuccess
	Data         *TargetList `json:"data"`
	CheckedCount int         `json:"checkedCount"`
}

type CheckedOrganCount struct {
	Date      string `json:"checkedDate" gorm:"Column:checkedDate"`
	Count     int    `json:"count" gorm:"Column:count"`
	OrganType int    `json:"organType" gorm:"Column:organType"`
}

type CheckedPatientCount struct {
	Date  string `json:"checkedDate" gorm:"Column:checkedDate"`
	Count int    `json:"count" gorm:"Column:count"`
}

type CheckedCountList struct {
	Date                  string               `json:"checkedDate"`
	PatientCount          int                  `json:"patientCount"`
	CheckedOrganCountList []*CheckedOrganCount `json:"checkedOrganCountList"`
	CheckedCount          int                  `json:"checkedCount"`
}

type CheckedCountListSwagger struct {
	*SwaggerSuccess
	Data []*CheckedCountList `json:"data"`
}

func StatisticsCheckedOrganCount(db *gorm.DB, beginTime, endTime string) ([]*CheckedOrganCount, error) {
	var list []*CheckedOrganCount
	query := "select organType, count(organType) as count, DATE(createTime) as checkedDate "
	query += "from t_organ_info where patientId >= 0 "
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf("and createTime >= %v and createTime <= %v ", beginTime, endTime)
	}
	query += "group by checkedDate, organType"
	res := db.Raw(query).Scan(&list)
	if res.Error != nil {
		return nil, res.Error
	}
	return list, nil
}

func StatisticsCheckedPatientCount(db *gorm.DB, beginTime, endTime string) ([]*CheckedPatientCount, error) {
	var list []*CheckedPatientCount

	query := "select count(distinct(patientId)) as count, DATE(createTime) as checkedDate "
	query += "from t_organ_info where patientId >= 0 "
	if beginTime != "" && endTime != "" {
		query += fmt.Sprintf("and createTime >= %v and createTime <= %v ", beginTime, endTime)
	}
	query += "group by checkedDate"

	res := db.Raw(query).Scan(&list)

	if res.Error != nil {
		return nil, res.Error
	}
	return list, nil
}
